﻿function SelCity(obj,e) {
    var ths = obj;
    var thcity = $("#hproperName");
    var dal = '<div class="_citys"><span title="关闭" id="cColse" >×</span><ul id="_citysheng" class="_citys0"><li class="citySel">省份</li><li>城市</li></ul><div id="_citys0" class="_citys1"></div><div style="display:none" id="_citys1" class="_citys1"></div></div>';
    Iput.show({ id: ths, event: e, content: dal,width:"470"});
    $("#cColse").click(function () {
        Iput.colse();
    });
    var tb_province = [];
    var b = province;
    for (var i = 0, len = b.length; i < len; i++) {
        tb_province.push('<a data-level="0" data-id="' + b[i]['id'] + '" data-name="' + b[i]['name'] + '">' + b[i]['name'] + '</a>');
    }
    
    $("#_citys0").append(tb_province.join(""));
    
    $("#_citys0 a").click(function () {
        var selid = $(this).data("id");
        if(selid == '110100' || selid == '310100' || selid == '120100' || selid == '500100' || selid == '710001'|| selid == '820001'|| selid == '810001'){
            var lev = $(this).data("name");
            if (document.getElementById("hproper") == null) {
                var hcitys = $('<input>', {
                    type: 'hidden',
                    name: "hproper",
                    "data-name": lev,
                    id: "hproper",
                    val: $(this).data("id")
                });
                $(ths).after(hcitys);
            }
            else {
                $("#hproper").attr("data-name", lev);
                $("#hproper").val($(this).data("id"));
            }
            var bc = $("#hcity").val();
            thcity.val($(this).data("name"));
            Iput.colse();
            $.session.set('hproper', $("#hproper").data("name"));
            $.session.set('hproper-id', $("#hproper").val());
            $("#fomCity").submit();
        } else {
            var g = getCity($(this));
            $("#_citys1 a").remove();
            $("#_citys1").append(g);
            $("._citys1").hide();
            $("._citys1:eq(1)").show();
            $("#_citys0 a,#_citys1 a").removeClass("AreaS");
            $(this).addClass("AreaS");
            var lev = $(this).data("name");
            if (document.getElementById("hcity") == null) {
                var hcitys = $('<input>', {
                    type: 'hidden',
                    name: "hcity",
                    "data-name": lev,
                    id: "hcity",
                    val: $(this).data("id")
                });
                $(ths).after(hcitys);
            }
            else {
                $("#hcity").val($(this).data("id"));
                $("#hcity").attr("data-id", lev);
            }
        }

        $("#_citys1 a").click(function () {
            $("#_citys1 a").removeClass("AreaS");
            $(this).addClass("AreaS");
            var lev =  $(this).data("name");
            if (document.getElementById("hproper") == null) {
                var hcitys = $('<input>', {
                    type: 'hidden',
                    name: "hproper",
                    "data-name": lev,
                    id: "hproper",
                    val:$(this).data("id")
                });
                $(ths).after(hcitys);
            }
            else {
                $("#hproper").attr("data-name", lev);
                $("#hproper").val($(this).data("id"));
            }
            var bc = $("#hcity").val();
            thcity.val($(this).data("name"));
            Iput.colse();
            $.session.set('hproper', $("#hproper").data("name"));
            $.session.set('hproper-id', $("#hproper").val());
            $("#fomCity").submit();
        });
    });
    $("#_citysheng li").click(function () {
        $("#_citysheng li").removeClass("citySel");
        $(this).addClass("citySel");
        var s = $("#_citysheng li").index(this);
        $("._citys1").hide();
        $("._citys1:eq(" + s + ")").show();
    });
}

function getCity(obj) {
    var c = obj.data('id');
    var e = province;
    var f;
    var g = '';
    for (var i = 0, plen = e.length; i < plen; i++) {
        if (e[i]['id'] == parseInt(c)) {
            f = e[i]['city'];
            break
        }
    }
    for (var j = 0, clen = f.length; j < clen; j++) {
        g += '<a data-level="1" data-id="' + f[j]['id'] + '" data-name="' + f[j]['name'] + '" title="' + f[j]['name'] + '">' + f[j]['name'] + '</a>'
    }
    $("#_citysheng li").removeClass("citySel");
    $("#_citysheng li:eq(1)").addClass("citySel");
    return g;
}
function getArea(obj) {
    var c = obj.data('id');
    var e = area;
    var f = [];
    var g = '';
    for (var i = 0, plen = e.length; i < plen; i++) {
        if (e[i]['pid'] == parseInt(c)) {
            f.push(e[i]);
        }
    }
    for (var j = 0, clen = f.length; j < clen; j++) {
        g += '<a data-level="1" data-id="' + f[j]['id'] + '" data-name="' + f[j]['name'] + '" title="' + f[j]['name'] + '">' + f[j]['name'] + '</a>'
    }

    $("#_citysheng li").removeClass("citySel");
    $("#_citysheng li:eq(2)").addClass("citySel");
    return g;
}